package com.lapian.eventbus;

import android.content.Intent;
import android.os.Bundle;
import android.renderscript.Matrix2f;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.lapian.eventbus.bean.MessageEvent;
import com.lapian.eventbus.bean.StickyEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends AppCompatActivity {

    private TextView tvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initDate();
    }

    private void initDate() {
        EventBus.getDefault().register(MainActivity.this);
    }

    public void OnShowActivityClick(View view) {
        startActivity(new Intent(MainActivity.this,MainActivity2.class));
    }

    public void OnSendStickyEventsClick(View view) {
        //2.发送粘性事件
        EventBus.getDefault().postSticky(new StickyEvent("我是粘性事件"));
        startActivity(new Intent(MainActivity.this, MainActivity2.class));

    }

    private void initView() {
        tvResult = (TextView) findViewById(R.id.tv_result);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解注册
        EventBus.getDefault().unregister(MainActivity.this);
    }
    //5.接收消息
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MessageEventBus(MessageEvent event){
        //显示接收的消息
        tvResult.setText(event.name);
    }
}